import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { PageQuery, PageResult } from '@/types/common';

/**
 * 美容项目查询对象
 */
export interface ProjectQuery extends PageQuery {
  projectName?: string;
  category?: string;
  hospitalName?: string;
  status?: string;
  minPrice?: number;
  maxPrice?: number;
  beginTime?: string;
  endTime?: string;
}

/**
 * 美容项目业务对象
 */
export interface ProjectForm {
  projectId?: number;
  projectName: string;
  category: string;
  price: number;
  originalPrice?: number;
  description?: string;
  detail?: string;
  image?: string;
  images?: string;
  tags?: string;
  salesCount?: number;
  sortOrder?: number;
  status?: string;
  overdueFeeRate?: number; // 逾期费率/固定金额
  overdueFeeType?: string; // 逾期费用类型：1-按比例，2-固定金额
  hospitalIds?: number[]; // 关联的医院ID列表
  installmentPlans?: Array<{
    periods: number;
    interestRate: number;
    serviceFeeRate: number;
    isRecommend: boolean;
  }>; // 分期方案列表
}

/**
 * 美容项目视图对象
 */
export interface ProjectVO {
  projectId: number;
  projectName: string;
  category: string;
  price: number;
  originalPrice: number;
  hospitalCount: number;
  description: string;
  detail: string;
  image: string;
  images: string;
  imageList: string[];
  tags: string;
  tagList: string[];
  salesCount: number;
  sortOrder: number;
  status: string;
  overdueFeeRate: number; // 逾期费率/固定金额
  overdueFeeType: string; // 逾期费用类型
  isFavorite: boolean;
  createTime: string;
  discountRate: number;
}

/**
 * 查询美容项目列表
 */
export function listProject(query: ProjectQuery): AxiosPromise<PageResult<ProjectVO>> {
  return request({
    url: '/admin/beauty/project/list',
    method: 'get',
    params: query
  });
}

/**
 * 查询美容项目详细
 */
export function getProject(projectId: number): AxiosPromise<ProjectVO> {
  return request({
    url: '/admin/beauty/project/' + projectId,
    method: 'get'
  });
}

/**
 * 新增美容项目
 */
export function addProject(data: ProjectForm): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/project',
    method: 'post',
    data: data
  });
}

/**
 * 修改美容项目
 */
export function updateProject(data: ProjectForm): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/project',
    method: 'put',
    data: data
  });
}

/**
 * 删除美容项目
 */
export function delProject(projectIds: number | number[]): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/project/' + projectIds,
    method: 'delete'
  });
}

/**
 * 上架项目
 */
export function onlineProject(projectId: number): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/project/online/' + projectId,
    method: 'put'
  });
}

/**
 * 下架项目
 */
export function offlineProject(projectId: number): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/project/offline/' + projectId,
    method: 'put'
  });
}

/**
 * 批量上架项目
 */
export function batchOnlineProject(projectIds: number[]): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/project/batchOnline',
    method: 'put',
    data: projectIds
  });
}

/**
 * 批量下架项目
 */
export function batchOfflineProject(projectIds: number[]): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/project/batchOffline',
    method: 'put',
    data: projectIds
  });
}

/**
 * 导出美容项目
 */
export function exportProject(query: ProjectQuery): AxiosPromise<Blob> {
  return request({
    url: '/admin/beauty/project/export',
    method: 'post',
    params: query,
    responseType: 'blob'
  });
}

/**
 * 查询项目关联的医院ID列表
 */
export function getProjectHospitals(projectId: number): AxiosPromise<number[]> {
  return request({
    url: `/admin/beauty/project/hospitals/${projectId}`,
    method: 'get'
  });
}

/**
 * 查询项目的分期方案列表
 */
export function getProjectInstallmentPlans(projectId: number): AxiosPromise<any[]> {
  return request({
    url: `/admin/beauty/project/installmentPlans/${projectId}`,
    method: 'get'
  });
}
